import std.functional : toDelegate;

import vibe.core.core : runApplication;
import vibe.http.fileserver;
import vibe.http.router;
import vibe.http.server;

import controller.index_ctrl;
import controller.article_ctrl;
import controller.category_ctrl;
import controller.search_ctrl;
import controller.about_ctrl;
import controller.source_ctrl;

import controller.admin_ctrl;

import utils.my_jwt;

void main ()
{
    auto settings = new HTTPServerSettings;
    settings.port = 8181;
    settings.bindAddresses = ["127.0.0.1"];
    settings.errorPageHandler = toDelegate(&errorHandler);

    auto router = new URLRouter;
    router.get("/", &getIndexPage); // 首页
    router.get("*", serveStaticFiles("./public/",)); // 静态资源
    router.get("/article/:id", &getArticleDetailPage); // 文章详情
    router.get("/category/:catename/articles", &getCateArticlesPage); // 分类文章列表
    router.get("/category/list", &getCategotyList); // 分类列表数据
    router.get("/search", &getArticleSearchPage); // 搜索文章
    router.get("/archive", &getArticleArchivePage); // 归档
    router.get("/about", &getAboutPage); // 关于页
    router.get("/source", &getSourcePage); // 资源页
    router.get("/admin/login", &getAdminLoginPage); // 后台登录页
    router.post("/admin/login", &handleLoginPost); // 后台登录提交
    router.get("/admin/index", &getAdminIndexPage); // 后台首页
    router.get("/admin/console1", &getAdminConsolePage); // 后台控制台1
    router.get("/admin/article/write", &getWriteArticlePage); // 后台写文章页
    router.get("/admin/article/manage", &getArticleManagePage); // 后台文章管理页
    router.get("/admin/category/manage", &getCategoryManagePage); // 后台分类管理页

    // token校验
    // 此后面的路由认证后才能访问
    router.any("*", &jwtValidator);
    router.get("/admin/menu", &getAdminMenu); // 后台菜单数据
    router.post("/admin/article/write", &handlePublishArticlePost); // 后台写文章提交
    router.get("/admin/article/list", &getArticleManageList); // 后台文章管理列表数据
    router.put("/admin/article/edit", &handleEditArticlePut); // 后台编辑文章提交
    router.delete_("/admin/article/delete/:id", &handleArticleDelete); // 后台删除文章
    router.post("/admin/article/batchDelete", &handleArticleBatchDelete); // 后台批量删除文章
    router.post("/admin/category/add", &handleAddCategoryPost); // 后台添加分类
    router.put("/admin/category/edit", &handleEditCategoryPut); // 后台编辑分类提交
    router.delete_("/admin/category/delete/:id", &handleCategoryDelete); // 后台删除分类
    router.post("/admin/category/batchDelete", &handleCategoryBatchDelete); // 后台批量删除分类

    auto server = listenHTTP(settings, router);
    scope (exit)
    {
        server.stopListening();
    }

    runApplication();
}

/** 
 * 路由错误处理函数
 */
void errorHandler (HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error)
{
    import std.format : format;
    import std.algorithm.searching : canFind;
    import vibe.data.json : Json;

    if (req.method == HTTPMethod.POST || req.method == HTTPMethod.PUT ||
        canFind(req.headers["Accept"], "application/json") ||
        canFind(req.headers["Content-Type"], "application/json"))
    {
        Json resData = Json.emptyObject;
        resData["code"] = error.code;
        resData["msg"] = error.message;
        resData["data"] = null;
        return res.writeJsonBody(resData, error.code);
    }

    string text = format("<html><body><h1>%s  (%d)</h1></body></html>",
        error.message, error.code);
    res.writeBody(text, "text/html; charset=utf-8");
}
